<?php
/**
 * 微信接口测试文件
 * 用于测试微信公众号接口是否工作正常
 */

// 加载WordPress环境
define('WP_USE_THEMES', false);
// 修复路径引用问题 - 多种方式尝试加载 wp-load.php
$wp_load_paths = array(
    dirname(dirname(dirname(__FILE__))) . '/wp-load.php',
    $_SERVER['DOCUMENT_ROOT'] . '/wp-load.php',
    '/www/wwwroot/www.ailingxi.cn/wp-load.php' // 硬编码绝对路径作为最后手段
);

$loaded = false;
foreach ($wp_load_paths as $path) {
    if (file_exists($path)) {
        require_once($path);
        $loaded = true;
        // 记录成功加载的路径到文件
        file_put_contents(__DIR__ . '/logs/path_debug.log', date('Y-m-d H:i:s') . ' - Test: Loaded: ' . $path . "\n", FILE_APPEND);
        break;
    }
}

if (!$loaded) {
    // 记录所有尝试过的路径
    $debug_msg = date('Y-m-d H:i:s') . " - Test: Failed to load wp-load.php, tried:\n";
    foreach ($wp_load_paths as $path) {
        $debug_msg .= "- $path (exists: " . (file_exists($path) ? 'yes' : 'no') . ")\n";
    }
    file_put_contents(__DIR__ . '/logs/path_debug.log', $debug_msg, FILE_APPEND);
    
    die('无法加载WordPress环境，请联系管理员检查路径配置。');
}

// 设置日志路径
$log_file = __DIR__ . '/logs/test.log';

// 记录日志函数
function test_log($message) {
    global $log_file;
    $log_message = '[' . date('Y-m-d H:i:s') . '] ' . $message . "\n";
    file_put_contents($log_file, $log_message, FILE_APPEND);
}

// 记录测试开始
test_log('=== 测试开始 ===');
test_log('请求IP: ' . $_SERVER['REMOTE_ADDR']);
test_log('User-Agent: ' . $_SERVER['HTTP_USER_AGENT']);

// 检查插件是否激活
$active_plugins = get_option('active_plugins', array());
$plugin_activated = false;
foreach ($active_plugins as $plugin) {
    if (strpos($plugin, 'hywl-wechat') !== false) {
        $plugin_activated = true;
        break;
    }
}

test_log('插件激活状态: ' . ($plugin_activated ? '已激活' : '未激活'));

// 获取插件设置
$options = get_option('hywl_wechat_options', array());
test_log('插件设置: ' . json_encode($options, JSON_UNESCAPED_UNICODE));

// 创建一个模拟的微信消息
$xml = <<<XML
<xml>
  <ToUserName><![CDATA[gh_xxxxxx]]></ToUserName>
  <FromUserName><![CDATA[oxxxxxx]]></FromUserName>
  <CreateTime>12345678</CreateTime>
  <MsgType><![CDATA[text]]></MsgType>
  <Content><![CDATA[测试关键词]]></Content>
  <MsgId>1234567890123456</MsgId>
</xml>
XML;

test_log('模拟发送微信消息: ' . $xml);

// 模拟POST请求
$url = home_url('/wechat-api/');
test_log('请求URL: ' . $url);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$error = curl_error($ch);
curl_close($ch);

test_log('HTTP状态码: ' . $http_code);
if (!empty($error)) {
    test_log('CURL错误: ' . $error);
}
test_log('响应内容: ' . $response);

// 测试URL重写规则
$rules = get_option('rewrite_rules');
$found_rule = false;
foreach ($rules as $pattern => $query) {
    if (strpos($pattern, 'wechat-api') !== false) {
        $found_rule = true;
        test_log('找到重写规则: ' . $pattern . ' => ' . $query);
    }
}

if (!$found_rule) {
    test_log('未找到wechat-api的重写规则');
    
    // 尝试刷新规则
    test_log('尝试刷新重写规则');
    flush_rewrite_rules();
    
    // 再次检查
    $rules = get_option('rewrite_rules');
    foreach ($rules as $pattern => $query) {
        if (strpos($pattern, 'wechat-api') !== false) {
            test_log('刷新后找到重写规则: ' . $pattern . ' => ' . $query);
        }
    }
}

// 检查日志文件
$log_dir = __DIR__ . '/logs/';
$wechat_log_files = glob($log_dir . 'wechat-*.log');
if (empty($wechat_log_files)) {
    test_log('未找到微信日志文件');
} else {
    test_log('找到以下微信日志文件:');
    foreach ($wechat_log_files as $log_file) {
        test_log('- ' . basename($log_file) . ' (' . filesize($log_file) . ' 字节)');
    }
}

// 检查调试日志
$debug_log = $log_dir . 'debug.log';
if (file_exists($debug_log)) {
    test_log('调试日志大小: ' . filesize($debug_log) . ' 字节');
} else {
    test_log('未找到调试日志文件');
}

// 显示测试结果
echo '<h1>微信接口测试结果</h1>';
echo '<pre>';
echo file_get_contents($log_file);
echo '</pre>';

// 提供额外的调试信息
echo '<h2>WordPress信息</h2>';
echo '<ul>';
echo '<li>WordPress版本: ' . get_bloginfo('version') . '</li>';
echo '<li>站点URL: ' . get_bloginfo('url') . '</li>';
echo '<li>微信接口URL: ' . home_url('/wechat-api/') . '</li>';
echo '</ul>';

// 添加直接测试链接
echo '<h2>测试链接</h2>';
echo '<ul>';
echo '<li><a href="' . home_url('/wechat-api/?test=1') . '" target="_blank">测试接口响应</a></li>';
echo '<li><a href="' . admin_url('admin.php?page=hywl-wechat-logs') . '" target="_blank">查看日志</a></li>';
echo '</ul>';

test_log('=== 测试结束 ==='); 